package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.CoreLibrary;
import com.baomidou.ant.entity.SysUser;
import com.baomidou.ant.mapper.CoreLibraryMapper;
import com.baomidou.ant.service.ICoreLibraryService;
import com.baomidou.ant.service.ISysDeptService;
import com.baomidou.ant.service.ISysUserService;
import com.baomidou.ant.vo.request.RequestListCoreVo;
import com.baomidou.ant.vo.response.ResponseListCoreVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@Service
public class CoreLibraryServiceImpl extends ServiceImpl<CoreLibraryMapper, CoreLibrary> implements ICoreLibraryService {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;

    @Override
    public IPage<ResponseListCoreVo> listCore(RequestListCoreVo vo) {
        IPage<CoreLibrary> iPage=new Page<>(vo.getDisplayStart(),vo.getDisplayLength());
        QueryWrapper<CoreLibrary> queryWrapper=new QueryWrapper<>();
        if(ObjectUtils.isNotEmpty(vo.getCorePin())){
            queryWrapper.eq("core_pin",vo.getCorePin());
        }
        SysUser user=userService.getById(vo.getUserId());
        //判断管理员级别
        if(Objects.equals(user.getType(), "1")){
            //管理员条件查询
            if(StringUtils.isNotBlank(vo.getDeptName())){
                Integer deptId=deptService.queryByDeptName(vo.getDeptName());
                queryWrapper.eq("dept_id",deptId);
            }
        }else if(Objects.equals(user.getType(), "0")||Objects.equals(user.getType(), "2")){//二级管理员,只能看自己部门的
            queryWrapper.eq("dept_id",user.getDeptId());
        }
        if(ObjectUtils.isNotEmpty(vo.getCoreStatus())){
            queryWrapper.eq("core_status",vo.getCoreStatus());
        }
        IPage<CoreLibrary> page=page(iPage,queryWrapper);
        return page.convert(coreLibrary -> {
            ResponseListCoreVo responseListCoreVo=new ResponseListCoreVo();
            BeanUtils.copyProperties(coreLibrary,responseListCoreVo);
            responseListCoreVo.setDeptName(deptService.getById(coreLibrary.getDeptId()).getDept());
            responseListCoreVo.setPinName("通用");
            return responseListCoreVo;
        });
    }

    @Override
    public CoreLibrary queryByCorePin(String corePin, Integer deptId) {
        QueryWrapper<CoreLibrary> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("core_pin",corePin);
        queryWrapper.eq("dept_id",deptId);
        return getOne(queryWrapper);
    }


    @Override
    public CoreLibrary queryByCoreCode(String coreCode) {
        QueryWrapper<CoreLibrary> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("core_code",coreCode);
        return getOne(queryWrapper);
    }


}
